//
//  HeadlinesViewController.swift
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/9.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class HeadlinesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: .Plain)
    var dataArray: NSMutableArray = []
    var page: NSInteger = 0
    var isPulling: Bool = false
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.title = "今日头条"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.page = 1
        createData()
        createTableView()
        createRefresh()
    }

    func createData() {
        if isPulling {
            self.dataArray.removeAllObjects()
        }
        let manager = AFHTTPRequestOperationManager()
        let parameters = ["brightUrl": "",
                            "catUrl": "toutiao",
                            "exceptMallUrl": "",
                            "homeShow": "0",
                            "infoNation": "0",
                            "infoType": "0",
                            "isTop": "1",
                            "keyWord": "",
                            "mallUrl": "",
                            "pageIndex": self.page,
                            "pageSize": "20",
                            "tagUrl": "",
                         ]
        manager.POST(headlines_url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            guard let array = responseObject as? [[String: AnyObject]] else {
                return
            }
            for dict: [String: AnyObject] in array {
                let model = TxcModel(dict: dict)
                self.dataArray.addObject(model)
            }
            self.tableView.reloadData()
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            SVProgressHUD.showErrorWithStatus(AppNetErrorTips)
        }
    }
    
    func createTableView() {
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
        tableView.rowHeight = 120
        tableView.showsVerticalScrollIndicator = false
        let nib = UINib(nibName: "HeadlinesCell", bundle: NSBundle.mainBundle())
        tableView.registerNib(nib, forCellReuseIdentifier: "headlinesCell")
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return HeadlinesCell.headlinesCellWith(tableView, indexPath: indexPath, dataArray: self.dataArray)
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let headlinesDetialVC = HeadlinesDetailViewController()
        guard let model = self.dataArray[indexPath.row] as? TxcModel else {
            print("向详情页传值错误")
            return
        }
        guard let infoID = model.InfoID else {
            return
        }
        headlinesDetialVC.goodsIDH = "\(infoID)"
        headlinesDetialVC.parametersH = ["infoID": infoID]
        headlinesDetialVC.pic_urlH = model.MainImage
        self.navigationController?.pushViewController(headlinesDetialVC, animated: true)
    }
    
    func createRefresh() {
        tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "downRefresh")
        tableView.mj_footer = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: "upRefresh")
    }
    
    func downRefresh() {
        self.isPulling = true
        self.page = 1
        tableView.mj_header.beginRefreshing()
        self.createData()
        tableView.mj_header.endRefreshing()
    }
    
    func upRefresh() {
        isPulling = false
        self.page++
        tableView.mj_footer.beginRefreshing()
        self.createData()
        tableView.mj_footer.endRefreshing()
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
